using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using Yoozoo.Gameplay.City;

namespace Yoozoo.Gameplay.CityEditor
{
    [CustomEditor(typeof(City4xFogComponent))]
    public class City4xFogComponentEditor : UnityEditor.Editor
    {
        private City4xFogComponent _fogComponent;
        private void OnEnable()
        {
            _fogComponent = target as City4xFogComponent;
        }

        public override void OnInspectorGUI()
        {
            base.OnInspectorGUI();

            int areaCount = _fogComponent.AreaCount;

            GUILayout.Label("按顺序解锁");
            int groupCount = (areaCount + 2) / 3;
            int areaIndex = 1;
            for (int i = 0; i < groupCount; i++)
            {
                GUILayout.BeginHorizontal();
                do
                {
                    if (GUILayout.Button("区域" + areaIndex, GUILayout.Height(30)))
                    {
                        _fogComponent.UnlockAreaFogEditor(areaIndex);
                    }

                    areaIndex++;
                } while (areaIndex <= areaCount && areaIndex <= (i + 1) * 3);

                GUILayout.EndHorizontal();
            }

            GUILayout.Label("使用遮罩图");
            areaIndex = 1;
            for (int i = 0; i < groupCount; i++)
            {
                GUILayout.BeginHorizontal();
                do
                {
                    if (GUILayout.Button("区域" + areaIndex, GUILayout.Height(30)))
                    {
                        _fogComponent.UnlockAreaFogByTex(areaIndex);
                    }

                    areaIndex++;
                } while (areaIndex <= areaCount && areaIndex <= (i + 1) * 3);

                GUILayout.EndHorizontal();
            }

            if (GUILayout.Button("构建遮罩图", GUILayout.Height(30)))
            {
                _fogComponent.maskTexturePaths = new List<string>();
                TextAsset areaJson =
                    AssetDatabase.LoadAssetAtPath<TextAsset>("Assets/ResourcesAssets/City/Config/areaJsonConfig.json");
                _fogComponent.Init(areaJson);
                _fogComponent.BuildBeginMask();
                areaCount = _fogComponent.AreaCount;

                {
                    byte[] bytes = _fogComponent.fogMaskTex.EncodeToPNG();
                    var dirPath = Application.dataPath + _fogComponent.maskTexPath;
                    if (!Directory.Exists(dirPath))
                    {
                        Directory.CreateDirectory(dirPath);
                    }

                    string p = dirPath + "tex_cityFogMask_0" + ".png";
                    File.WriteAllBytes(p, bytes);
                    AssetDatabase.Refresh();

                    _fogComponent.maskTexturePaths.Add($"Assets{_fogComponent.maskTexPath}tex_cityFogMask_0.png");
                }

                for (int i = 1; i <= areaCount; i++)
                {
                    _fogComponent.UnlockAreaFog(i);
                    byte[] bytes = _fogComponent.fogMaskTex.EncodeToPNG();
                    var dirPath = Application.dataPath + _fogComponent.maskTexPath;
                    if (!Directory.Exists(dirPath))
                    {
                        Directory.CreateDirectory(dirPath);
                    }

                    string p = dirPath + "tex_cityFogMask_" + i + ".png";
                    File.WriteAllBytes(p, bytes);
                    AssetDatabase.Refresh();

                    _fogComponent.maskTexturePaths.Add($"Assets{_fogComponent.maskTexPath}tex_cityFogMask_{i}.png");
                }

                _fogComponent.fogMaskTex = null;
                EditorUtility.SetDirty(_fogComponent);
            }
        }

        // public void Serialize(object t, string path)
        // {
        //     using(Stream stream = File.Open(path, FileMode.Create))
        //     {
        //         BinaryFormatter bformatter = new BinaryFormatter();
        //         bformatter.Serialize(stream, t);
        //     }
        // }
        //
        // public object Deserialize(string path) 
        // {
        //     using(Stream stream = File.Open(path, FileMode.Open))
        //     {
        //         BinaryFormatter bformatter = new BinaryFormatter();
        //         return bformatter.Deserialize(stream);
        //     }
        // }
    }
}
